package com.ruoyi.web.controller.module.estate;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.EstateCallCenterRecord;
import com.ruoyi.system.service.module.estate.IEstateCallCenterRecordService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 呼叫记录Controller
 *
 * @author ruoyi
 * @date 2024-03-02
 */
@Validated
@Api(value = "呼叫记录控制器", tags = {"呼叫记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/estate/call/center/record")
public class EstateCallCenterRecordController extends BaseController {

    private final IEstateCallCenterRecordService estateCallCenterRecordService;

    /**
     * 查询呼叫记录列表
     */
    @ApiOperation("查询呼叫记录列表")
    @PreAuthorize("@ss.hasPermi('module:callCenterRecord:list')")
    @GetMapping("/list")
    public TableDataInfo<EstateCallCenterRecord> list(@Validated EstateCallCenterRecord estateCallCenterRecord) {
        QueryWrapper<EstateCallCenterRecord> qw = new QueryWrapper<>();
		if(estateCallCenterRecord.getEstateId()!=null){
			qw.eq("estate_id",estateCallCenterRecord.getEstateId());
		}
		if (estateCallCenterRecord.getCenterId()!=null){
			qw.eq("center_id",estateCallCenterRecord.getCenterId());
		}
		qw.orderByDesc("id");
        TableDataInfo<EstateCallCenterRecord> td = PageUtils.buildDataInfo(estateCallCenterRecordService.page(PageUtils.buildPage(),qw));
        List<EstateCallCenterRecord> list = td.getRows();
        return td;
    }

    /**
     * 导出呼叫记录列表
     */
    @ApiOperation("导出呼叫记录列表")
    @PreAuthorize("@ss.hasPermi('module:callCenterRecord:export')")
    @Log(title = "呼叫记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EstateCallCenterRecord> export(@Validated EstateCallCenterRecord estateCallCenterRecord) {
        List<EstateCallCenterRecord> list = estateCallCenterRecordService.list();
        ExcelUtil<EstateCallCenterRecord> util = new ExcelUtil<EstateCallCenterRecord>(EstateCallCenterRecord.class);
        return util.exportExcel(list, "呼叫记录");
    }

    /**
     * 获取呼叫记录详细信息
     */
    @ApiOperation("获取呼叫记录详细信息")
    @PreAuthorize("@ss.hasPermi('module:callCenterRecord:query')")
    @GetMapping("/{id}")
    public AjaxResult<EstateCallCenterRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstateCallCenterRecord estateCallCenterRecord = estateCallCenterRecordService.getById(id);
        return AjaxResult.success(estateCallCenterRecord);
    }

	/**
	 * 获取呼叫记录详细信息
	 */
	@ApiOperation("呼叫记录状态修改")
	@PreAuthorize("@ss.hasPermi('module:callCenterRecord:query')")
	@GetMapping("/updateState")
	public AjaxResult<EstateCallCenterRecord> updateState(@Validated EstateCallCenterRecord estateCallCenterRecord) {
		QueryWrapper<EstateCallCenterRecord> qw = new QueryWrapper<>();
		Date beginDate = new Date();
		Date endDate = new Date();
		beginDate.setTime(beginDate.getTime()-180000);
		qw.between("create_time",beginDate,endDate);
//		qw.eq("call_phone",phone);
		qw.eq("state",0);
		List<EstateCallCenterRecord> records = estateCallCenterRecordService.list(qw);
		if (records.size()>0){
			for (EstateCallCenterRecord record:records){
				record.setState(1);
				estateCallCenterRecordService.updateById(record);
			}
		}
		return null;
	}

    /**
     * 新增呼叫记录
     */
    @ApiOperation("新增呼叫记录")
    @PreAuthorize("@ss.hasPermi('module:callCenterRecord:add')")
    @Log(title = "呼叫记录", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EstateCallCenterRecord estateCallCenterRecord) {
        return toAjax(estateCallCenterRecordService.save(estateCallCenterRecord) ? 1 : 0);
    }

    /**
     * 修改呼叫记录
     */
    @ApiOperation("修改呼叫记录")
    @PreAuthorize("@ss.hasPermi('module:callCenterRecord:edit')")
    @Log(title = "呼叫记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EstateCallCenterRecord estateCallCenterRecord) {
        return toAjax(estateCallCenterRecordService.updateById(estateCallCenterRecord) ? 1 : 0);
    }

    /**
     * 删除呼叫记录
     */
    @ApiOperation("删除呼叫记录")
    @PreAuthorize("@ss.hasPermi('module:callCenterRecord:remove')")
    @Log(title = "呼叫记录" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(estateCallCenterRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
